<!--
 * @Author: White
 * @description: 添加逻辑
 * @LastEditTime: 2021-08-19 18:02:40
-->

<template>
    <div>
        <div class="title">添加代办事项</div>
        <div class="tools">
            <input type="inputValue" v-model="inputValue" placeholder="搜索" @keydown.enter="add(inputValue)">
            <button @click="add(inputValue)">新增</button>
        </div>
        <ul>
            <li v-for="(item, index) in todos" :key="index">
                <span class="desc">{{item}}</span>
                <span class="down" @click.prevent="check(index)">完成</span>
                <span class="del" @click="deleteItem(index)">删除</span>
            </li>
        </ul>
    </div>
</template>

<script lang="ts">
import { ref, reactive, computed, defineComponent } from 'vue'
import store from '@/store'

export default defineComponent({
    setup () {
        const inputValue = ref()
        // 添加事项
        const add = (value: string) => {
            if (value.trim() === '') return
            store.commit('add', value)
            inputValue.value = ''
        }
        // 删除事项
        const deleteItem = (index: number) => {
            store.commit('delete', index)
        }
        // 完成该事项
        const check = (index: number) => {
            store.commit('check', index)
        }
        return reactive({
            inputValue,
            add,
            deleteItem,
            check,
            todos: computed(() => store.state.todos)
        })
    }
})
</script>

<style scoped lang="scss">
.title {
    font-size: 14px;
    margin-bottom: 20px;
}
.tools {
    display: flex;
    align-items: center;
    justify-content: center;
    input {
        height: 20px;
        border: none;
        border: 1px solid gray;
        border-radius: 20px 00px 0px 20px
    }
    button {
        height: auto;
        border-radius: 0px 20px 20px 0px;
        border: 1px solid gray;
    }
}
li {
    display: flex;
    align-items: center;
    height: 30px;
    background: rgba( 255, 255, 255, 0.55 );
    box-shadow: 0 2px 12px 0 rgba( 31, 38, 135, 0.37 );
    backdrop-filter: blur( 4px );
    -webkit-backdrop-filter: blur( 4px );
    border-radius: 10px;
    border: 1px solid rgba( 255, 255, 255, 0.18 );
    margin: 20px 10px;
    padding: 0px 10px;
    .desc {
        width: 60%;
    }
    .down {
        width: 20%;
        font-size: 10px;
        height: 20px;
        line-height: 20px;
        background-color: #ff5556;
        color: #fff;
        border-radius: 10px;
        padding: 0px 2px;
        margin: 0px 10px;
    }
    .del {
        width: 20%;
        font-size: 10px;
        height: 20px;
        line-height: 20px;
        background-color: #f77543;
        color: #fff;
        border-radius: 10px;
        padding: 0px 2px;
    }
}
</style>